package com.whsxt.servlet;

import cn.hutool.core.util.StrUtil;
import com.whsxt.common.Constant;
import com.whsxt.common.Result;
import com.whsxt.pojo.User;
import com.whsxt.service.CustomerService;
import com.whsxt.service.impl.CustomerServiceImpl;
import com.whsxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 武汉尚学堂
 */
@WebServlet("/customer.do")
public class CustomerServlet extends HttpServlet {

    private CustomerService customerService = new CustomerServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String service = req.getParameter("service");
        try {
            //通过反射调用方法
            Method m = this.getClass().getDeclaredMethod(service, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 分页查询客户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page"); //页码
        String limit = req.getParameter("limit"); //每页数据条数  layui 会默认传输的参数
        //获取查询参数
        String name  = req.getParameter("name");
        String phone  = req.getParameter("phone");
        String company = req.getParameter("company");
        String userId = req.getParameter("userId");
        //将查询参数进行封装
        Map<String,String> params = new HashMap<>();
        if (StrUtil.isNotBlank(name)){
            params.put("name",name);
        }
        if (StrUtil.isNotBlank(phone)){
            params.put("phone",phone);
        }
        if (StrUtil.isNotBlank(company)){
            params.put("company",company);
        }
        if (StrUtil.isNotBlank(userId)){
            params.put("userId",userId);
        }
        //获取员工数据
        Result rs = customerService.queryPage(params,page,limit);
        //将数据返回
        RespUtil.writer(resp,rs);
    }

    /**
     * 新增客户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String name  = req.getParameter("name");
        String phone  = req.getParameter("phone");
        String company = req.getParameter("company");
        String sex = req.getParameter("sex");
        //新增客户数据
        //获取当前业务员ID
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        Integer userId = user.getId();
        Result rs = customerService.addCustomer(name,phone,company,sex,userId);
        //将数据返回
        RespUtil.writer(resp,rs);
    }

    /**
     * 修改客户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id  = req.getParameter("id");
        String name  = req.getParameter("name");
        String phone  = req.getParameter("phone");
        String company = req.getParameter("company");
        String sex = req.getParameter("sex");
        //修改客户数据
        Result rs = customerService.updateCustomer(id,name,phone,company,sex);
        //将数据返回
        RespUtil.writer(resp,rs);
    }


    /**
     * 根据ID删除客户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id  = req.getParameter("id");
        //删除客户数据
        Result rs = customerService.deleteCustomer(id);
        //将数据返回
        RespUtil.writer(resp,rs);
    }

    /**
     * 设置客户的业务员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String userId  = req.getParameter("userId");
        String[] customerIds = req.getParameterValues("customerId");
        //批量修改customer的userId的值
        Result rs = customerService.setCsutomerUserId(userId,customerIds);
        //将数据返回
        RespUtil.writer(resp,rs);
    }
}
